package com.dao;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.base.BaseDao;
import com.base.PageBean;
import com.bean.ParkOrder;
import com.bean.Userinfo;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.User;
import com.util.GUtil;

/**
 	* A data access object (DAO) providing persistence and search support for Userinfo entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.bean.Userinfo
  * @author MyEclipse Persistence Tools 
 */
public class UserinfoDao extends BaseDao {
	private static final Logger log = Logger.getLogger(UserinfoDao.class);
	// property constants
	public static final String PHONE = "phone";
	public static final String USER_PASSWD = "userPasswd";
	public static final String CAR_NO = "carNo";
	public static final String REAL_NAME = "realName";
	public static final String INTRODUCTION = "introduction";
	public static final String USER_PHOTO = "userPhoto";
	public static final String ID_CARD = "idCard";
	public static final String ACCOUNT_BALANCE = "accountBalance";
	public static final String SCORE = "score";
	public static final String CREDIT = "credit";
	public static final String PAY_PASSWD = "payPasswd";
	public static final String GENDER = "gender";
	public static final String EMAIL = "email";
	public static final String CLIENT_TYPE = "clientType";
	public static final String UDID = "udid";
	public static final String LOGIN_SEQUENCE = "loginSequence";
	public static final String USER_TOKEN = "userToken";
	public static final String CLIENT_ID = "clientId";
	public static final String STATUS = "status";
	
    
    public String save(Userinfo userinfo) {
        log.debug("saving Userinfo instance");
        try {
            getSession().save(userinfo);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
        return GUtil.SUCCESS;
    }
    public Userinfo findById( java.lang.Long id) {
        log.debug("getting Userinfo instance with id: " + id);
        try {
            Userinfo instance = (Userinfo) getSession()
                    .get(Userinfo.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
	  public List findByProperty(String propertyName, Object value) {
	      log.debug("finding Userinfo instance with property: " + propertyName
	            + ", value: " + value);
	      try {
	         String queryString = "from Userinfo as model where model." 
	         						+ propertyName + "= ?";
	         Query queryObject = getSession().createQuery(queryString);
			 queryObject.setParameter(0, value);
			 return queryObject.list();
	      } catch (RuntimeException re) {
	         log.error("find by property name failed", re);
	         throw re;
	      }
		}

	public String delete(Userinfo userinfo) {
        log.debug("deleting Userinfo instance");
        try {
            getSession().delete(userinfo);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
        return GUtil.SUCCESS;
    }
    
    
    
    public List findByExample(Userinfo instance) {
        log.debug("finding Userinfo instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.dao.Userinfo")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
	public String delete(Long id, Integer flag){
		Userinfo userinfo = this.findById(id);
		if(userinfo!=null){
			return delete(userinfo, flag);
		}else{
			return GUtil.FAILE;
		}
	}
	public String lock(Long id, Integer flag){
		Userinfo userinfo = this.findById(id);
		if(userinfo!=null){
			return delete(userinfo, flag);
		}else{
			return GUtil.FAILE;
		}
	}
	public String delete(Userinfo userinfo,Integer flag) {
		log.debug("deleting ParkOrder instance");
		try {
			if(flag==null)
				getSession().delete(userinfo);
			else{
				userinfo.setStatus(flag.shortValue());
				getSession().update(userinfo);
			}
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
		return GUtil.SUCCESS;
	}
	
	public List findByPhone(Object phone
	) {
		return findByProperty(PHONE, phone
		);
	}
	
	public List findByUserPasswd(Object userPasswd
	) {
		return findByProperty(USER_PASSWD, userPasswd
		);
	}
	
	public List findByCarNo(Object carNo
	) {
		return findByProperty(CAR_NO, carNo
		);
	}
	
	public List findByRealName(Object realName
	) {
		return findByProperty(REAL_NAME, realName
		);
	}
	
	public List findByIntroduction(Object introduction
	) {
		return findByProperty(INTRODUCTION, introduction
		);
	}
	
	public List findByUserPhoto(Object userPhoto
	) {
		return findByProperty(USER_PHOTO, userPhoto
		);
	}
	
	public List findByIdCard(Object idCard
	) {
		return findByProperty(ID_CARD, idCard
		);
	}
	
	public List findByAccountBalance(Object accountBalance
	) {
		return findByProperty(ACCOUNT_BALANCE, accountBalance
		);
	}
	
	public List findByScore(Object score
	) {
		return findByProperty(SCORE, score
		);
	}
	
	public List findByCredit(Object credit
	) {
		return findByProperty(CREDIT, credit
		);
	}
	
	public List findByPayPasswd(Object payPasswd
	) {
		return findByProperty(PAY_PASSWD, payPasswd
		);
	}
	
	public List findByGender(Object gender
	) {
		return findByProperty(GENDER, gender
		);
	}
	
	public List findByEmail(Object email
	) {
		return findByProperty(EMAIL, email
		);
	}
	
	public List findByClientType(Object clientType
	) {
		return findByProperty(CLIENT_TYPE, clientType
		);
	}
	
	public List findByUdid(Object udid
	) {
		return findByProperty(UDID, udid
		);
	}
	
	public List findByLoginSequence(Object loginSequence
	) {
		return findByProperty(LOGIN_SEQUENCE, loginSequence
		);
	}
	
	public List findByUserToken(Object userToken
	) {
		return findByProperty(USER_TOKEN, userToken
		);
	}
	
	public List findByClientId(Object clientId
	) {
		return findByProperty(CLIENT_ID, clientId
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	public List findAll() {
		log.debug("finding all Userinfo instances");
		try {
			String queryString = "from Userinfo";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List<Userinfo> findchenge(Userinfo userinfo,PageBean page) {
		session = getSession();
		log.debug("finding all Userinfo instances");
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("from Userinfo where status <> -1 ");
			if(userinfo!=null&&!"".equals(userinfo)){
				Short status=userinfo.getStatus();
				if(userinfo.getStatus()!=null&&!"".equals(userinfo.getStatus())){
					sb.append(" and status='"+status+"'");
				}
				
				if(userinfo.getRealName()!=null&&!"".equals(userinfo.getRealName())){
					sb.append(" and realName like'%"+ userinfo.getRealName()+ "%'");			
				}
				
				if(userinfo.getCarNo()!=null&&!"".equals(userinfo.getCarNo())){
					sb.append(" and carNo like'%"+ userinfo.getCarNo()+ "%'");	
				}
			}
			Long count = (Long) session.createQuery("select count(*) "+ sb.toString()).uniqueResult();
			if(page!=null && count!=null )
				page.setTotalRow(count.intValue());
				Query queryObject = session.createQuery(sb.toString());
				
				if(page!=null && page.getPageSize()>0 && page.getCurrentPage()>0){
					queryObject.setFirstResult( (page.getCurrentPage()-1)*page.getPageSize());
					queryObject.setMaxResults( page.getPageSize() );
				}
			
			List<Userinfo> list = queryObject.list();
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<Userinfo> findststus(){
		log.debug("finding all Userinfo instances");
		try {
			String queryString = "from Userinfo group by status";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public String update(Userinfo userinfo) {
        log.debug("merging Userinfo instance");
        try {
            getSession().update(userinfo);
            log.debug("merge successful");
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
        return GUtil.SUCCESS;
    }

    public void attachDirty(Userinfo instance) {
        log.debug("attaching dirty Userinfo instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Userinfo instance) {
        log.debug("attaching clean Userinfo instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}